
package ru.ifmo.mlarinsky.classificator.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.*;
import javax.swing.border.LineBorder;
import net.miginfocom.swing.MigLayout;

public class LegendSectorPanel extends JPanel {
    private static final Dimension LABEL_SIZE = new Dimension(20, 20);
    private static final int BORDER_THICKNESS = 1;

    private final String sector;
    private final Color color;

    public LegendSectorPanel(String sector, Color color) {
        super(new MigLayout("fill, gap 15"));

        this.sector = sector;
        this.color = color;

        initLayout();
    }

    private void initLayout() {
        JLabel colorLabel = new JLabel() {

            @Override
            public void paint(Graphics g) {
                super.paint(g);

                Graphics2D g2D = (Graphics2D) g.create();
                g2D.setColor(color);
                g2D.fillRect(BORDER_THICKNESS,
                        BORDER_THICKNESS,
                        getWidth() - 2 * BORDER_THICKNESS,
                        getHeight() - 2 * BORDER_THICKNESS);
                g2D.dispose();
            }
        };
        colorLabel.setBorder(new LineBorder(Color.BLACK, BORDER_THICKNESS));
        colorLabel.setPreferredSize(LABEL_SIZE);
        colorLabel.setMaximumSize(LABEL_SIZE);
        colorLabel.setMinimumSize(LABEL_SIZE);

        JLabel sectorLabel = new JLabel(sector);

        add(colorLabel, "left");
        add(sectorLabel, "left");
        add(new JPanel(), "grow, pushx, right");
    }

    public static void main(String[] args) {
        testLegendSectorPanel();
    }

    private static void testLegendSectorPanel() {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                LegendSectorPanel panel = new LegendSectorPanel("Test Sector", Color.GREEN);

                JPanel contentPane = new JPanel(new MigLayout("fill, ins 0"));
                contentPane.add(panel, "center");

                JFrame mainFrame = new JFrame();
                mainFrame.setContentPane(contentPane);
                mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                mainFrame.setSize(new Dimension(200, 100));
                mainFrame.setVisible(true);
            }
        });
    }
}
